﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Web.Old_App_Code;

public partial class backstage_admin_set : System.Web.UI.Page
{
    // Fields
    protected Admin ad = new Admin();
    protected AdminGroupManager agm = new AdminGroupManager();
    protected AdminManager am = new AdminManager();
   

    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            DataTable adminById = this.am.GetAdminById(Convert.ToInt32((this.Session["admin_user"] as Admin).Id));
            this.ad.UserName = this.txtUserName.Value;
            if (this.txtUserPwd.Value != "")
            {
                this.ad.UserPwd = new DataEncryption().MD5Encrypt(this.txtUserPwd.Value);
            }
            this.ad.Email = this.txtEmail.Value;
            this.ad.Name = this.txtName.Value;
            this.ad.Depart = this.txtDepart.Value;
            this.ad.GroupId = Convert.ToInt32(adminById.Rows[0]["adminGroup"]);
            this.ad.State = Convert.ToInt32(adminById.Rows[0]["state"]);
            this.ad.RegisterIP = new ClinetMAC().GetIPAddress();
            this.ad.Id = Convert.ToInt32(adminById.Rows[0]["id"]);
            if (this.am.ModifyAdminInfo(this.ad))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"admin_set.aspx\";</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
            }
        }
        catch
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack)
        {
            this.UserRankBind();
        }
    }

    private void UserRankBind()
    {
        try
        {
            DataTable adminById = this.am.GetAdminById(Convert.ToInt32((this.Session["admin_user"] as Admin).Id));
            if (adminById.Rows.Count > 0)
            {
                this.txtUserName.Value = adminById.Rows[0]["userName"].ToString();
                this.txtUserName.Attributes.Add("readonly", "readonly");
                this.txtEmail.Value = adminById.Rows[0]["email"].ToString();
                this.txtName.Value = adminById.Rows[0]["name"].ToString();
                this.txtDepart.Value = adminById.Rows[0]["depart"].ToString();
            }
        }
        catch
        {
        }
    }

}